package top.drinyeit.nongmao.presenter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.List;
import java.util.Map;

import top.drinyeit.nongmao.activity.CommodityDetailNewActivity;
import top.drinyeit.nongmao.adapter.CategoryAdapter;
import top.drinyeit.nongmao.adapter.StoreAllAdapter;
import top.drinyeit.nongmao.fragment.StoreNewFragment;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/10.
 */

public class StoreNewPresenter {
    private StoreNewFragment fragment;
    private StoreAllAdapter storeAllAdapter;
    private CategoryAdapter categoryAdapter;
    public StoreNewPresenter(StoreNewFragment fragment){
        this.fragment = fragment;
    }
    //获取新上架宝贝
    public void getStoreAll(Map<String,Object> map) {
        CommonSubscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<CategoryGoodsDetailsBean>>>() {
            @Override
            public void onError(String e, int code) {
                fragment.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(final HttpResponse<List<CategoryGoodsDetailsBean>> o) {
                if (o.getCode() == COMMON_SUCCESS) {
                    categoryAdapter = new CategoryAdapter(o.getData(),null);
                    fragment.mRecyclerView.setLayoutManager(new LinearLayoutManager(fragment.getActivity()));
                    fragment.mRecyclerView.setAdapter(categoryAdapter);
                    if (categoryAdapter != null && categoryAdapter.getData().size() > 0){
                        categoryAdapter.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, final int position) {
                                Intent intent = new Intent(fragment.getActivity(), CommodityDetailNewActivity.class);
                                intent.putExtra("id", categoryAdapter.getData().get(position).getId());
                                fragment.getActivity().startActivity(intent);
                            }
                        });
                    }
                }else {
                    fragment.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getStoreAll(subscriber, map);
    }
}
